//
//  BSSAddTeacherHomeViewController.m
//  PreschoolEducation
//
//  Created by 李传铎 on 2020/11/15.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSAddTeacherHomeViewController.h"
#import "BSSAddTeacherOneViewController.h"
#import "BSSAddTeacherTwoViewController.h"
#import "BSSAddTeacherThreeViewController.h"
#import "BSSAddTeacherFourViewController.h"
@interface BSSAddTeacherHomeViewController ()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) UIImageView *stateImageV;
@property (nonatomic ,assign) NSInteger currentIndex;
@end

@implementation BSSAddTeacherHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatUI];
}
- (void)creatUI{
    _currentIndex = 0;

    UIView *diView = [[UIView alloc]initWithFrame:CGRectMake(0, __kNavigationBarHeight__, SCREEN_WIDTH, 92)];
    [self.view addSubview:diView];
    diView.backgroundColor = [UIColor whiteColor];
    UIImage *image = imageNameStr(@"pic_four_first");
    _stateImageV = [[UIImageView alloc]init];
    [diView addSubview:_stateImageV];
    _stateImageV.image = image;
    float left = (SCREEN_WIDTH - image.size.width)/2;
    [_stateImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(diView.mas_centerY);
        make.left.mas_equalTo(left);
        make.width.mas_equalTo(image.size.width);
        make.height.mas_equalTo(image.size.height);
    }];
    
    [self.view addSubview:self.scrollView];
   
    BSSAddTeacherOneViewController * oneVC = [[BSSAddTeacherOneViewController alloc]init];;
    [self addChildViewController:oneVC];
    BSSAddTeacherTwoViewController * twoVC = [[BSSAddTeacherTwoViewController alloc]init];;
    [self addChildViewController:twoVC];
    BSSAddTeacherThreeViewController * threeVC = [[BSSAddTeacherThreeViewController alloc]init];;
    [self addChildViewController:threeVC];
    BSSAddTeacherFourViewController * fourVC = [[BSSAddTeacherFourViewController alloc]init];;
    [self addChildViewController:fourVC];
    BSSAddTeacherOneViewController *vc = self.childViewControllers[0];
    vc.view.frame = CGRectMake(0 *self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
    [self.scrollView addSubview:vc.view];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH *2, 0);
    
    UIButton *previousBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:previousBtn];
    previousBtn.layer.cornerRadius = 16;
    previousBtn.layer.borderWidth = 1;
    previousBtn.layer.borderColor = colorCode(@"#979797").CGColor;
    [previousBtn setTitle:@"上一步" forState:0];
    previousBtn.titleLabel.font = fontCount(14);
    [previousBtn setTitleColor:colorCode(@"#999999") forState:0];
    [previousBtn addTarget:self action:@selector(previousClick) forControlEvents:UIControlEventTouchUpInside];
    
    [previousBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(32);
        make.bottom.mas_equalTo(-16);
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(-67.5);
    }];
    
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:nextBtn];
    nextBtn.layer.cornerRadius = 16;
    nextBtn.layer.masksToBounds = YES;
    nextBtn.backgroundColor = colorCode(@"#FE7778");
    [nextBtn setTitle:@"下一步" forState:0];
    nextBtn.titleLabel.font = fontCount(14);
    [nextBtn setTitleColor:colorCode(@"#ffffff") forState:0];
    [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
    
    [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(32);
        make.bottom.mas_equalTo(-16);
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(67.5);
    }];
   
    
}
- (void)previousClick{
    if (self.currentIndex > 0) {
        self.currentIndex --;
    }
    WEAKSELF;
    [UIView animateWithDuration:0.5 animations:^{
        CGFloat offsetX =  SCREEN_WIDTH * self.currentIndex;
        CGFloat offsetY = weakSelf.scrollView.contentOffset.y;
        CGPoint offset = CGPointMake(offsetX, offsetY);
        [weakSelf.scrollView setContentOffset:offset animated:YES];
    }];
    if (self.currentIndex == 0) {
        _stateImageV.image = imageNameStr(@"pic_four_first");
    }else if (self.currentIndex == 1){
        _stateImageV.image = imageNameStr(@"pic_four_secend");
    }else if (self.currentIndex == 2){
        _stateImageV.image = imageNameStr(@"pic_four_trhird");
    }else if (self.currentIndex == 3){
        _stateImageV.image = imageNameStr(@"pic_four_fourth");
    }
}
- (void)nextClick{
    if (self.currentIndex < 3) {
        self.currentIndex ++;
    }
    WEAKSELF;
    [UIView animateWithDuration:0.5 animations:^{
        CGFloat offsetX =  SCREEN_WIDTH * self.currentIndex;
        CGFloat offsetY = weakSelf.scrollView.contentOffset.y;
        CGPoint offset = CGPointMake(offsetX, offsetY);
        [weakSelf.scrollView setContentOffset:offset animated:YES];
    }];
    if (self.currentIndex == 0) {
        _stateImageV.image = imageNameStr(@"pic_four_first");
    }else if (self.currentIndex == 1){
        _stateImageV.image = imageNameStr(@"pic_four_secend");
    }else if (self.currentIndex == 2){
        _stateImageV.image = imageNameStr(@"pic_four_trhird");
    }else if (self.currentIndex == 3){
        _stateImageV.image = imageNameStr(@"pic_four_fourth");
    }
}
/** 滚动结束后调用（代码导致） */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / self.scrollView.frame.size.width;
    
   
    if (index == 0) {
        BSSAddTeacherOneViewController *newsVc = self.childViewControllers[index];
        
        if (newsVc.view.superview) return;
        newsVc.view.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, scrollView.frame.size.height);
        [self.scrollView addSubview:newsVc.view];
    }else if (index == 1){
        BSSAddTeacherTwoViewController *newsVc = self.childViewControllers[index];
        
        if (newsVc.view.superview) return;
        newsVc.view.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, scrollView.frame.size.height);
        [self.scrollView addSubview:newsVc.view];
    }else if (index == 2){
        BSSAddTeacherThreeViewController *newsVc = self.childViewControllers[index];
        
        if (newsVc.view.superview) return;
        newsVc.view.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, scrollView.frame.size.height);
        [self.scrollView addSubview:newsVc.view];
    }else if (index == 3){
        BSSAddTeacherFourViewController *newsVc = self.childViewControllers[index];
        
        if (newsVc.view.superview) return;
        newsVc.view.frame = CGRectMake(index*SCREEN_WIDTH, 0, SCREEN_WIDTH, scrollView.frame.size.height);
        [self.scrollView addSubview:newsVc.view];
    }
    
    
    
    
}
/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

/** 正在滚动 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, __kNavigationBarHeight__ + 92, SCREEN_WIDTH, SCREEN_HEIGHT - __kNavigationBarHeight__ - 40 - 65 - 92)];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        self.scrollView.bounces = NO;
        self.scrollView.scrollEnabled = NO;
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointZero;
      
    }
    return _scrollView;
}
@end
